#include "stdafx.h"
#include <sstream>  

#include "MxUtils\MxError1.h"
#include "PluginICPopenCV\PluginICPopenCVErrorCodes.h"

#include "SumVal.h"


SumVal::SumVal(void) : 
	_refCnt(0)
{
}


SumVal::~SumVal(void)
{
}


bool		MXSTDMETHOD SumVal::Invoke(IImageCuratorFn *fn, bool DebugMode, bool DisplayMsg)
{
	bool rc = false; 

	if ((fn == nullptr) || (fn->IsValid() == false))
		MX_SETERROR(MX11116, MxError::InvalidParams,  MxError::Abort,  MxError::VerboseReport, "fn is nullptr, or invalid");
	else
	{
		if (   (fn->IsExistParam("ValA", IImageCuratorFnDef::ParamType::Integer, IImageCuratorFnDef::Direction::Input) == false) 
			|| (fn->IsExistParam("ValB", IImageCuratorFnDef::ParamType::Integer, IImageCuratorFnDef::Direction::Input) == false) 
			|| (fn->IsExistParam("Result", IImageCuratorFnDef::ParamType::Integer, IImageCuratorFnDef::Direction::Output) == false))
			MX_SETERROR(MX11117, MxError::BadUserInput,  MxError::Abort,  MxError::VerboseReport, "ValA, ValB or Result are not supported by this fn");
		else
		{
			int  res = Convert(fn->GetParamInt("ValA"), fn->GetParamInt("ValB"));
			if ( (fn->SetParamInt("Result", res)) == false)
				MX_SETERROR(MX11118, MxError::CodeDefect,  MxError::Abort,  MxError::QuietReport, "SetValue(Result, %d) failed", res);
			else
			{
				rc = true;
			}
		}
	}
	return rc;
}

long		MXSTDMETHOD SumVal::DestroyPtr(void)
{
	long cnt = InterlockedDecrement(&_refCnt);
	if ( cnt == 0 )
		delete this;
	return cnt;
}

long		MXSTDMETHOD SumVal::DuplicatePtr(void)
{
	return InterlockedIncrement(&_refCnt);
}

void *		MXSTDMETHOD SumVal::Dynamic_cast(const GUID IId)
{
	void *rc = nullptr;

	if ( MxGUID::IsEqual(IId, IMxPluginBaseIID::Guid()))
		rc = static_cast<IMxPluginBase *>(this);
	else
		rc = nullptr;

	std::string tmp;
	if (rc == nullptr)
		MX_SETERROR(MX11119, MxError::Install, MxError::Abort, MxError::QuietReport, "Dynamic_cast does not support IID=%s", MxGUID::ConvertToString(IId, &tmp));
	else
		((IMxPluginBase *)rc)->DuplicatePtr();

	return rc;
}


int MXSTDMETHOD SumVal::Convert(int valA, int valB)
{
	int rc = -1;

	rc = valA + valB;

	return rc;
}